<template>
<!--修改账户-->
  <div class="container" v-show="show">
    <el-dialog
      title="修改密码"
      v-model:visible="dialogVisible"
      width="40%">
      <div>
        <el-divider></el-divider>
      </div>
    <div>
      <el-form  :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item class="is-required" label="旧密码" prop="age" >
          <el-input v-model.number="ruleForm.age" placeholder="请输入旧密码"></el-input>
        </el-form-item>
        <el-form-item class="is-required" label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" placeholder="请输入新密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  class="is-required" label="确认新密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" placeholder="请确认新密码" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false" size="mini">关闭</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')" size="mini">确认</el-button>
        </el-form-item>
      </el-form>
    </div>
    </el-dialog>
  </div>
</template>

<script>
//import { ElDialog, ElForm, ElFormItem, ElInput, ElButton, ElDivider } from 'element-ui'
export default {
  name: 'editAccount',
 
  props: {
    show: {
      type: Boolean,
      default: true
    }
  },
  data () {
    const checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('密码不能为空'))
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'))
        }
      }, 1000)
    }
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: true,
      ruleForm: {
        pass: '',
        checkPass: '',
        age: ''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        age: [
          { validator: checkAge, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-dialog__title {
  font-size: 16px !important;
}
::v-deep .el-dialog__body {
  padding: 0 20px 10px;
}
::v-deep .el-divider--horizontal {
  margin: 0 0 24px;
}
</style>
